﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rInvoice.Utils;

namespace rInvoice
{
    public partial class BankInputPanel : Form
    {
        private ServerObject mServerObject = null;
        int mBankID = 0;
        string mBankCod = string.Empty;
        string mBankAccount = string.Empty;
        string mBankName = string.Empty;
        
        bool mIsActive = true;
        
        bool mCreatingNew = false;

        public bool CreateNew
        {
            get {  return mCreatingNew;  }
            set {  mCreatingNew = value; }
        }

        public int BankID
        {
            get { return mBankID; }
            set { mBankID = value; }
        }

        public string BankCod
        {
            get { return mBankCod; }
            set { mBankCod = value; }
        }

        public string BankAccount
        {
            get { return mBankAccount; }
            set { mBankAccount = value; }
        }

        public string BankName
        {
            get { return mBankName; }
            set { mBankName = value; }
        }


        public BankInputPanel()
        {
            InitializeComponent();
            mServerObject = new ServerObject();
        }

        private void BankInputPanel_Load(object sender, EventArgs e)
        {
            if (!mCreatingNew)
            {
                bankCodTextBox.Text = mBankCod;
                bankAccountTextBox.Text = mBankAccount;
                bankNameTextBox1.Text = mBankName;
                activeCheckBox.Checked = mIsActive;
            }
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            bool operationResult = false;

            if (mCreatingNew)
            {
                if (mServerObject.AddBank(mBankCod,mBankAccount,mBankName))
                {
                    operationResult = true;
                }
            }
            else
            {
                if (mServerObject.UpdateBank(mBankID, mBankCod, mBankAccount, mBankName, !mIsActive))
                {
                    operationResult = true;
                }
            }

            if (operationResult) 
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            else
                this.DialogResult = System.Windows.Forms.DialogResult.Abort;

        }        

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Abort;
        }

        private void bankCodTextBox_TextChanged(object sender, EventArgs e)
        {
            mBankCod = bankCodTextBox.Text.Trim();
        }

        private void bankAccountTextBox_TextChanged(object sender, EventArgs e)
        {
            mBankAccount = bankAccountTextBox.Text.Trim();
        }

        private void bankNameTextBox1_TextChanged(object sender, EventArgs e)
        {
            mBankName = bankNameTextBox1.Text.Trim();
        }

        private void activeCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            mIsActive = activeCheckBox.Checked;
        }

    }
}
